#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kernel.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/joystick.h>
#include <linux/input.h>
#include <linux/major.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/poll.h>
#include <linux/device.h>
#include <linux/cdev.h>

/**
mknod /dev/mycdev002 c  410 0

*/

#define MY_MAJOR       410
#define MY_MAX_MINORS  1

typedef struct {
	struct cdev cdev;
	u8 mem[1024];
} my_device_data;

my_device_data devs[MY_MAX_MINORS];

static int my_open(struct inode *inode, struct file *filp) {
	my_device_data *my_data;
	pr_info("a3 my_open\n");
	my_data = container_of(inode->i_cdev,  my_device_data, cdev);
	filp->private_data = my_data;
	return 0;
}

static ssize_t my_write(struct file *filp,
		const char __user *user_buffer,
		size_t size,
		loff_t *offset)
{
	//pr_info("write");
	 my_device_data *my_data;
	size_t my_size = 1024;
	size_t len = min(my_size - (size_t)(*offset), size);
	my_data = filp->private_data;
	if (len <= 0)
	return 0;

	if (copy_from_user(my_data->mem + *offset, user_buffer, len))
	return -EFAULT;
	*offset += len;
	pr_info("write: %s",my_data->mem);
	return len;
}

static int r = 0;
 
static ssize_t my_read(struct file *filp,   
		char __user *buffer, 
		size_t count, 
		loff_t *offset)
{
	my_device_data *my_data = filp->private_data;
	u8 *data =  my_data-> mem;
	size_t datalen = strlen(data);
	int p = *offset;

	pr_info("a3 my_read: %d \n",r++);
	pr_info("length of the buffer : %zu \n",count);
	pr_info("offset : %lld \n",*offset);
	
	if(p > datalen) {
		return 0;
	}
	if (count > datalen - p ) {
		count = datalen - p;
	}
	if (copy_to_user(buffer, data+*offset, count)) {
		return -EFAULT;
	}
	*offset += count;
	return count;
}

const struct file_operations my_fops = {
		.owner = THIS_MODULE,
		.open = my_open, 
		.read = my_read,
		.write = my_write,
		};

static __init int hello_2_init(void) {
	int err;
	int i;
	pr_info("a3 init_module\n");
	err = register_chrdev_region(MKDEV(MY_MAJOR, 0), MY_MAX_MINORS,
			"my_device_driver");
	if (err != 0) {
		/* report error */
		return err;
	}

	for (i = 0; i < MY_MAX_MINORS; i++) {
		/* initialize devs[i] fields */
		cdev_init(&devs[i].cdev, &my_fops);
		cdev_add(&devs[i].cdev, MKDEV(MY_MAJOR, i), 1);
	}

	return 0;
}

static void __exit hello_2_exit(void) {
	int i;
	pr_info("a3 cleanup_module\n");
	for (i = 0; i < MY_MAX_MINORS; i++) {
		/* release devs[i] fields */
		cdev_del(&devs[i].cdev);
	}
	unregister_chrdev_region(MKDEV(MY_MAJOR, 0), MY_MAX_MINORS);
}

module_init(hello_2_init);
module_exit(hello_2_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy,81553652@qq.com");
MODULE_DESCRIPTION("A sample driver");
